<?php

class Excel_model extends CI_Model {

    function __construct() {
        parent::__construct();

        $this->_weixin_config = config_item("weixin");
    }

    /**
     * 获取指定的sheet的数据，返回数据
     * @param type $file 文件名
     * @param type $sheetId sheet序号
     * @param type $toColumn 到哪一列(字母)
     * @param type $fromRow 从哪一行开始
     * @param type $rows 选几行数据
     * @return type
     */
    public function getSheetData($file, $sheetId, $fromColumn = 'A', $fromRow = 1, $toColumn = false, $toRow = false) {
        include_once dirname(__FILE__) . '/../libraries/PHPExcel/IOFactory.php';
        //date_default_timezone_set('PRC');
        // 读取excel文件
        try {
            $inputFileType = \PHPExcel_IOFactory::identify($file);
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file);
        } catch (Exception $e) {
            throw new Exception('加载文件发生错误：”' . pathinfo($file, PATHINFO_BASENAME) . '”: ' . $e->getMessage());
        }

        $sheet = $objPHPExcel->getSheet($sheetId);
        $toRow = false === $toRow ? $sheet->getHighestRow() : $toRow;
        $toColumn = false === $toColumn ? $sheet->getHighestColumn() : $toColumn;

        $data = $sheet->rangeToArray($fromColumn . $fromRow . ':' . $toColumn . $toRow, NULL, TRUE, FALSE);
        return $data;
    }
    /*
    //filename 文件名称
    //filepath 文件路径
     * 
     */
    public function getData($filePath, $excelSetting) {
        $return = [];
        foreach ($excelSetting as $k => $v) {
            $return[$k] = $this->getSheetData($filePath, $v['sheet'], $v['from_col'], $v['from_row'], $v['to_col'], $v['to_row']);
        }

        return $return;
    }

}
